using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dimok.Data.DataSources;


namespace Data.DataSources {
	public class StatesDataSource : ObjectDataSourceBase {
		public enum AccessTypes { Read = 1, Update = 2, Delete = 4, Add = 8, List = 128 }
		private const string TABLE_NAME = "TableName";
		private const string ACCESS = "ACCESS";
		private string _svn = "v_State";
		public override string SelectViewName { get { return _svn; } set { _svn = value; } }
		public override string InsertViewName { get { return "t_State"; } }
		public string TableName{
			get {	return GetParameter(SelectParameters,TABLE_NAME)+"";
			}set{
				if(value == "") RemParameter(SelectParameters, TABLE_NAME);
				else {
					SetParameter(SelectParameters, TypeCode.String, TABLE_NAME, value);
				}
			}
		}
		public AccessTypes Access { set { SetSelectParameter(ACCESS, (int)value); } }
		public StatesDataSource(){
			EmtyRowPolicy = EmptyRowPolicyType.OnlyIfEmpty;
			OrderBy.Add("StateFlowPos");
		}
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);
			if(TableName != "" || SelectParameters.Count > 0)
				SelectMethod = "SelectByTable";
			Selecting += new ObjectDataSourceSelectingEventHandler(StatesDataSource_Selecting);
		}

		void StatesDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) {
			if (GetSelectParameter(TABLE_NAME) != null)
				if (GetSelectParameter(ACCESS) != null) {
					if (SelectMethod != "SelectByAccess") SelectMethod = "SelectByAccess";
					_svn = "fs_StatesByTable(@TableName,@Access)";
				} else
					if (SelectMethod != "SelectByTable") SelectMethod = "SelectByTable";

		}

		public DataView SelectByTable() { return SelectEx(); }
		public DataView SelectByTable(string TableName) {
			return Select("TableName=@TableName", TableName);
		}
		public DataView SelectByAccess(string TableName, AccessTypes access) {
			UseNoLock = false;
			return Select("", TableName, (int)access);
		}
	}
}